package com.pkk.cloud.support.gateway.provider;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import com.pkk.cloud.support.gateway.code.GatewayBusinessCode;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

/**
 * @description: eureka 服务熔断
 * @author: peikunkun
 * @create: 2019-05-29 16:07
 **/
@Component
public class EurekaClientFallback implements FallbackProvider {


  /**
   * @Description: // 表明是为哪个微服务提供回退，*表示为所有微服务提供回退
   * @return: java.lang.String
   * @Author: peikunkun
   * @Date: 2020/3/17 0017 下午 1:35
   */
  @Override
  public String getRoute() {
    // 表明是为哪个微服务提供回退，*表示为所有微服务提供回退
    return "*";
  }

  /**
   * @Description: 回退响应信息
   * @Param: route
   * @Param cause
   * @return: org.springframework.http.client.ClientHttpResponse
   * @Author: peikunkun
   * @Date: 2020/3/17 0017 下午 2:03
   */
  @Override
  public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
    if (cause instanceof HystrixTimeoutException) {
      return response(504);
    } else {
      return this.fallbackResponse();
    }
  }


  /**
   * @Description: 默认响应信息
   * @Param:
   * @return: org.springframework.http.client.ClientHttpResponse
   * @Author: peikunkun
   * @Date: 2020/3/17 0017 下午 2:03
   */
  public ClientHttpResponse fallbackResponse() {
    return this.response(500);
  }


  private ClientHttpResponse response(final int status) {
    return new ClientHttpResponse() {
      @Override
      public HttpStatus getStatusCode() {
        return HttpStatus.OK;
      }

      /**
       * @Description: 原始状态码信息
       * @return: int
       * @Author: peikunkun
       * @Date: 2020/3/17 0017 下午 2:04
       */
      @Override
      public int getRawStatusCode() {
        return HttpStatus.OK.value();
      }

      /**
       * @Description: 状态描述信息
       * @Param:
       * @return: java.lang.String
       * @Author: peikunkun
       * @Date: 2020/3/17 0017 下午 2:04
       */
      @Override
      public String getStatusText() {
        return HttpStatus.OK.getReasonPhrase();
      }

      @Override
      public void close() {
      }

      /**
       * @Description: 返回的body体
       * @return: java.io.InputStream
       * @Author: peikunkun
       * @Date: 2020/3/17 0017 下午 2:08
       */
      @Override
      public InputStream getBody() throws UnsupportedEncodingException {
        String str;
        if (status == 504) {
          str = JSONObject.toJSONString(ResponseUtil.error(GatewayBusinessCode.SERVICE_NO_REGISTER));
        } else {
          str = JSONObject.toJSONString(ResponseUtil.error(GatewayBusinessCode.SERVICE_NO_START));
        }
        return new ByteArrayInputStream(str.getBytes("UTF-8"));
      }

      /**
       * @Description: 头部信息
       * @return: org.springframework.http.HttpHeaders
       * @Author: peikunkun
       * @Date: 2020/3/17 0017 下午 2:06
       */
      @Override
      public HttpHeaders getHeaders() {
        // headers设定
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return headers;
      }
    };
  }
}